const app = getApp();

Page({
    data: {
        detailData: {},
        faultPhotos: []
    },
    onLoad(options) {
        const listId = options.orderId;
        this.getDetailData(listId);
    },
    // 获取详细信息数据
    getDetailData(listId) {
        console.log(listId);
        const token = app.globalData.token;
        if (!token) {
            wx.showToast({
                title: '未获取到有效token，请先登录',
                icon: 'none'
            });
            return;
        }

        const header = {
            'content-type': 'application/x-www-form-urlencoded',
            'token': token
        };

        const url = 'http://8.140.225.174:5000/fultlist_clearshow';
        const data = { ListId: listId };

        wx.request({
            url: url,
            method: 'POST',
            header: header,
            data: data,
            success: (res) => {
                if (res.data.code === 200 && res.data.data) {
                    this.setData({
                        detailData: res.data.data
                    });
                    this.getFaultPhotos(res.data.data.FaultPhoto);
                } else {
                    wx.showToast({
                        title: '获取详细信息失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error('网络错误：', err);
                wx.showToast({
                    title: '网络错误',
                    icon: 'none'
                });
            }
        });
    },
    // 获取故障图片数据
    getFaultPhotos(faultPhotoList) {
        const header = {
            'content-type': 'application/x-www-form-urlencoded',
            'token': app.globalData.token
        };

        const promises = faultPhotoList.map((photo) => {
            const url = 'http://8.140.225.174:5000/showphoto';
            const data = {
                filename: photo.filename,
                id: photo.id
            };

            return new Promise((resolve, reject) => {
                wx.request({
                    url: url,
                    method: 'POST',
                    header: header,
                    data: data,
                    responseType: 'arraybuffer',
                    success: (resSingle) => {
                        let imageData = resSingle.data;
                        // 使用wx.arrayBufferToBase64进行转换
                        const base64Image = wx.arrayBufferToBase64(imageData);
                        const base64Prefix = 'data:image/jpeg;base64,';
                        imageData = base64Prefix + base64Image;
                        resolve({
                            imageUrl: imageData,
                            loaded: false,
                            error: false
                        });
                    },
                    fail: (errSingle) => {
                        console.error('showphoto接口调用失败，错误信息：', errSingle);
                        reject(errSingle);
                    }
                });
            });
        });

        Promise.all(promises).then((photos) => {
            this.setData({
                faultPhotos: photos
            }, () => {
                console.log('faultPhotos after setData:', this.data.faultPhotos);
            });
        }).catch((error) => {
            console.error('获取故障图片错误：', error);
        });
    },
    handleImageLoad: function (e) {
        const index = e.currentTarget.dataset.index;
        console.log('index in handleImageLoad:', index);
        const faultPhotos = this.data.faultPhotos;
        console.log('faultPhotos in handleImageLoad:', faultPhotos);
        if (faultPhotos && faultPhotos.length > index && index >= 0) {
            faultPhotos[index].loaded = true;
            this.setData({ faultPhotos });
        } else {
            console.error('图片加载完成处理时，faultPhotos数组或索引存在问题', e);
        }
    },
    handleImageError: function (e) {
        const index = e.currentTarget.dataset.index;
        const faultPhotos = this.data.faultPhotos;
        faultPhotos[index].error = true;
        this.setData({ faultPhotos });
    }
})